package com.tedu.pj.sys.service.impl;

import com.tedu.pj.common.exception.ServiceException;
import com.tedu.pj.common.pojo.Node;
import com.tedu.pj.sys.dao.SysMenuDao;
import com.tedu.pj.sys.dao.SysRoleMenuDao;
import com.tedu.pj.sys.dao.SysUserRoleDao;
import com.tedu.pj.sys.pojo.SysMenu;
import com.tedu.pj.sys.pojo.SysUserMenu;
import com.tedu.pj.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/10/16
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    SysMenuDao sysMenuDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao ;

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Cacheable("menuCache")
    @Override
    public List<Map<String, Object>> findObjects() {
        List<Map<String, Object>> list = sysMenuDao.findObjects();
        if (list == null || list.size() == 0) {
            throw new ServiceException("没有对应的菜单信息");
        }
        return list;
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ)
    @Override
    public int deleteObject(Integer id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("请先选择");
        }
        int count = sysMenuDao.getChildCount(id);
        if (count > 0) {
            throw new ServiceException("请先删除子菜单");
        }
        sysRoleMenuDao.deleteObjectsByMenuId(id);
        int rows = sysMenuDao.deleteObject(id);
        if (rows == 0) {
            throw new ServiceException("此菜单可能已经不存在");
        }
        return rows;
    }

    @Override
    public List<Node> findZtreeMenuNodes() {
        return sysMenuDao.findZtreeMenuNodes();
    }

    @Override
    public int saveObject(SysMenu entity) {
        //1.合法验证
        if (entity == null) {
            throw new IllegalArgumentException("保存对象不能为空");
        }
        if (StringUtils.isEmpty(entity.getName())) {
            throw new IllegalArgumentException("菜单名不能为空");

        }
        int rows = sysMenuDao.insertObject(entity);
        return rows;
    }

    @Override
    public int updateObject(SysMenu entity) {
        //1.合法验证
        if (entity == null) {
            throw new ServiceException("保存对象不能为空");
        }
        if (StringUtils.isEmpty(entity.getName())) {
            throw new ServiceException("菜单名不能为空");
        }
        int rows = sysMenuDao.updateObject(entity);
        if (rows == 0) {
            throw new ServiceException("记录可能已经不存在");
        }
        return rows;
    }

    @Override
    public List<SysUserMenu> findUserMenusByUserId(Integer id) {
        List<Integer> roleIds = sysUserRoleDao.findRoleIdsByUserId(id);
        return sysMenuDao.findMenusByIds(sysRoleMenuDao.findMenuIdsByRoleIds(roleIds));
    }
}
